package com.glueball.crm.module.client.ui;

import com.glueball.crm.module.client.model.BankAccount;
import com.glueball.crm.module.client.model.Client;
import com.glueball.crm.module.client.model.ClientType;
import com.glueball.crm.module.client.model.Company;
import com.glueball.crm.module.client.model.Contact;
import com.glueball.crm.module.client.model.Person;
import com.glueball.crm.module.common.ui.BankAccountWidget;
import com.glueball.crm.module.common.ui.BankAccountWidgetFactory;
import com.glueball.crm.module.common.ui.ControllerWidget;
import com.glueball.crm.module.common.ui.CrmPanelWidget;
import com.glueball.crm.module.common.ui.EditableListWidget;
import com.glueball.crm.module.common.ui.ListWidget;
import com.glueball.crm.module.ui.CrmCompositeUI;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class ClientUI extends CrmCompositeUI {

	private static ClientUIUiBinder uiBinder = GWT.create(ClientUIUiBinder.class);

	private Client client = new Client();

	@UiField CaptionPanel clientDataPanel;
	//@UiField CaptionPanel contactsPanel;
	@UiField CaptionPanel contactsNamePanel;
	@UiField CaptionPanel contactsEmailPanel;
	@UiField CaptionPanel contactsPhonePanel;
	@UiField CaptionPanel bankAccountsPanel;

	interface ClientUIUiBinder extends UiBinder<Widget, ClientUI> {}

	public ClientUI(final Composite crmApp) {
		super(crmApp);
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setClient(final Client client) {
		this.client = client;
	}

	public Client getClient() {
		return this.client;
	}

	public void refresh() {
		try {
			// client data
			addClientDataWidget();
			// contacts
			addContactWidget();
			// bank accounts
			addBankAccountWidget();
			
		} catch (final Exception e) {
			Window.alert(this.getClass().getName()+"  "+e.getMessage());
		}
	}

	private void addClientDataWidget() {
		if (ClientType.PERSON.toString().equals(this.client.getType())) {
			if (this.client.getData() == null) {
				this.client.setData(new Person());
			}
			final PersonWidget personWidget = new PersonWidget(
								(Person) this.client.getData(),
								this,
								this.client.getId());
			this.clientDataPanel.clear();
			this.clientDataPanel.add(personWidget.asWidget());
//			this.clientDataPanel.setWidth("330px");
//			this.clientDataPanel.setHeight("205px");
		} else if (ClientType.COMPANY.toString().equals(this.client.getType())) {
			if (this.client.getData() == null) {
				this.client.setData(new Company());
			}
			final CompanyWidget companyWidget = new CompanyWidget(
								(Company) this.client.getData(),
								this,
								this.client.getId());
			this.clientDataPanel.clear();
			this.clientDataPanel.add(companyWidget.asWidget());
//			this.clientDataPanel.setWidth("330px");
//			this.clientDataPanel.setHeight("205px");
		}
	}

	@SuppressWarnings("unchecked")
	private void addContactWidget() {
		final ContactWidgetFactory contactWidgetFactory = new ContactWidgetFactory(
				URLHelper.SAVE_CONTACT + this.client.getId()
				);
		final ListWidget<Contact> conctactsList = new EditableListWidget<ContactWidget,Contact>(
				client.getContacts(), contactWidgetFactory);
		final CrmPanelWidget contactsBox = new CrmPanelWidget(conctactsList, "200px", "300px");
		contactsBox.addController(new ControllerWidget<ContactWidget>(
				URLHelper.getImageUrl("Refresh.png"), "Refresh",
				((EditableListWidget<ContactWidget,Contact>)conctactsList).getRefreshCallback()).asWidget()
				);
		contactsBox.addController(new ControllerWidget<ContactWidget>(
				URLHelper.getImageUrl("Add.png"), "Add",
				((EditableListWidget<ContactWidget,Contact>)conctactsList).getAddCallback()).asWidget()
				);
		this.contactsNamePanel.clear();
		this.contactsNamePanel.add(contactsBox.asWidget());		
	}

	@SuppressWarnings("unchecked")
	private void addBankAccountWidget() {
		final BankAccountWidgetFactory bankAccountWidgetFactory = new BankAccountWidgetFactory(
				URLHelper.SAVE_BANKACCOUNT+this.client.getId()
				);	
		final ListWidget<BankAccount> bankAccountsList = new EditableListWidget<BankAccountWidget,BankAccount>(
				client.getBankAccounts(),
				bankAccountWidgetFactory
				);
		final CrmPanelWidget bankAccountsBox = new CrmPanelWidget(bankAccountsList, "380px", "205px");
		bankAccountsBox.addController(new ControllerWidget<BankAccountWidget>(
				URLHelper.getImageUrl("Refresh.png"), "Refresh",
				((EditableListWidget<BankAccountWidget,BankAccount>)bankAccountsList).getRefreshCallback()).asWidget());
		bankAccountsBox.addController(new ControllerWidget<BankAccountWidget>(
				URLHelper.getImageUrl("Add.png"), "Add",
				((EditableListWidget<BankAccountWidget,BankAccount>)bankAccountsList).getAddCallback()).asWidget());
		this.bankAccountsPanel.clear();
		this.bankAccountsPanel.add(bankAccountsBox.asWidget());		
	}	
}
